#include "note.h"
#include "ui_note.h"

Note::Note(QWidget *parent) :
    BaseRestEditor(parent),
    ui(new Ui::Note)
{
    ui->setupUi(this);

    selectId=0;
    entry_=NC_NOTE;
    baseUrl_=URL_NC_NOTE;
    categories=QStringList();

    listModel=new QStringListModel(this);
    ui->listView->setModel(listModel);

    createMenu();
    updateCategory();
}

Note::~Note()
{
    delete ui;
}

void Note::generateMenu()
{
    menu->addAction(QStringLiteral("添加... (F1)"),this,&Note::newItem);
    menu->addSeparator();
    menu->addAction(QStringLiteral("编辑... (F2)"),this,&Note::editItem);
    menu->addSeparator();
    menu->addAction(QStringLiteral("更新类别 (F4)"),this,&Note::updateCategory);
    menu->addAction(QStringLiteral("更新视图 (F5)"),this,&Note::updateDetailView);
    menu->addSeparator();
    QMenu *optMenu=menu->addMenu("选项");
    optMenu->addAction(QStringLiteral("删除 (Delete)"),this,&Note::removeItem);
}

void Note::customKeyEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case Qt::Key_F1:
        newItem();
        break;
    case Qt::Key_F2:
        editItem();
        break;
    case Qt::Key_F4:
        updateCategory();
        break;
    case Qt::Key_F5:
        updateDetailView();
        break;
    case Qt::Key_Delete:
        removeItem();
        break;
    }
}

void Note::updateCategory()
{
    QString url=baseUrl_ + "get_categories";
    emit get(url);
}

void Note::updateListView()
{
    if(selectCategory.isEmpty())
        return;

    QString url = baseUrl_ + "get_labels/" + selectCategory;
    emit get(url);
}

void Note::updateDetailView()
{
    if(selectLabel.isEmpty())
        return;

    QString url = baseUrl_ + "by_label/" + selectLabel;
    emit get(url);
}

void Note::on_categories_currentTextChanged(const QString &category)
{
    selectCategory=category;
    updateListView();
}

void Note::on_listView_doubleClicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        selectLabel=index.data().toString();
        updateDetailView();
    }
}

void Note::newItem()
{
    AddNote addItem(this,categories);
    if(!selectCategory.isEmpty())
        addItem.setCategory(selectCategory);

    connect(&addItem,&AddNote::addItem,
            this,&Note::onAddItem);
    if(addItem.exec() == QDialog::Accepted)
    {
        delay();
        updateListView();
    }
}

void Note::editItem()
{
    if(selectId==0 || selectCategory.isEmpty() || selectLabel.isEmpty())
        return;

    QString detail=ui->detail->toPlainText();
    QString addition=ui->addition->toPlainText();

    AddNote addItem(this,categories);
    addItem.setItem(selectId,selectCategory,selectLabel,detail,addition);

    connect(&addItem,&AddNote::updateItem,this,&Note::onUpdateItem);
    if(addItem.exec() == QDialog::Accepted)
    {
        delay();
        updateDetailView();
    }
}

void Note::removeItem()
{
    if(selectId==0)
        return;

    QMessageBox::StandardButton btn=MESSAGE_DELETE_CONFIRM
            if(btn == QMessageBox::Ok)
    {

            QString url=baseUrl_ + "remove/" + QString::number(selectId);
            emit get(url);
            delay();
            updateListView();
}
}

void Note::onAddItem(const QJsonObject &obj)
{
    QString url=baseUrl_ + "insert";
    emit post(url,QJsonDocument(obj).toJson(QJsonDocument::Compact));
}

void Note::onUpdateItem(const QJsonObject &obj)
{
    QString url=baseUrl_ + "update";
    emit post(url,QJsonDocument(obj).toJson(QJsonDocument::Compact));
}

void Note::onReceivedObject(const QJsonObject &object)
{
    if(!object.contains("entry") || object.value("entry").toString() != entry_)
        return;

    if(object.contains("detail"))
    {
        QJsonArray array=object.value("detail").toArray();
        if(array.isEmpty())
            return;

        QJsonObject obj=array.first().toObject();
        if(obj.isEmpty())
            return;

        selectId=obj.value("id").toInt(0);
        QString detail=obj.value("detail").toString();
        ui->detail->setPlainText(detail);

        QString addition=obj.value("addition").toString();
        ui->addition->setPlainText(addition);
    }
    else if(object.contains("categories"))
    {
        QJsonArray array=object.value("categories").toArray();
        Piv piv(array);
        categories=piv.getStringList();
        if(ui->categories->count() != 0)
            ui->categories->clear();
        ui->categories->addItems(categories);
    }
    else if(object.contains("labels"))
    {
        QJsonArray array=object.value("labels").toArray();
        Piv piv(array);
        listModel->setStringList(piv.getStringList());
    }
}
